package cn.xiaosiyuan.temp;



import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo01 {

    public static void main(String[] args) {
        doBackup();
    }

    /**
     * 执行生成备份
     */
    public static void doBackup(){
        System.out.println("现在时间是"+new Date());
        Runtime runtime = Runtime.getRuntime();  //获取Runtime实例
        String database1 = "shop"; // 需要备份的数据库名
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String sdfDate = sdf.format(currentDate);
        String filepath = "d:\\time_" + sdfDate + ".sql"; // 备份的路径地址
        //执行命令
        String stmt = "d:\\mysqldump  --defaults-extra-file=d:\\my.ini "+database1+" > "+filepath;
//        System.out.println(stmt);
        try {
            String[] command = { "cmd", "/c", stmt};
//            Process process = runtime.exec(command);

            String[] commandRs = {"java -version","mvn -v"};
            Process process;
            InputStream input;
            InputStream errorStream;

            process = runtime.exec("java -version");
            input = process.getInputStream();
            System.out.println(IOUtils.toString(input, "UTF-8"));
            //若有错误信息则输出
            errorStream = process.getErrorStream();
            System.out.println(IOUtils.toString(errorStream, "gbk"));

            process = runtime.exec("cd D:\\life\\apache-maven-3.8.3");
            process = runtime.exec("mvn -v");
            input = process.getInputStream();
            System.out.println(IOUtils.toString(input, "UTF-8"));
            //若有错误信息则输出
            errorStream = process.getErrorStream();
            System.out.println(IOUtils.toString(errorStream, "gbk"));


        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
